package com.aviccxzx.smarthome.UserModule;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.utils.NetUtils;

/**
 * Created by joey on 2016/10/27.
 */
public class EnterWifiPwdActivity extends GosUserModuleBaseActivity implements View.OnClickListener {
    private EditText enter_wifiname_et;
    private EditText enter_wifipwd_et;
    String video;

    String name;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.enter_wifipwd_lay_v19);
        } else {
        }
        video = getIntent().getStringExtra("video");
        if (video.equals("video")) {
            ((TextView) findViewById(R.id.title_tv)).setText("添加摄像机");
        } else {
            ((TextView) findViewById(R.id.title_tv)).setText("添加智能网关");
        }
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.start_link_tv).setOnClickListener(this);
        enter_wifiname_et = (EditText) findViewById(R.id.enter_wifiname_et);
        enter_wifipwd_et = (EditText) findViewById(R.id.enter_wifipwd_et);
        setWifiName();
    }

    private void setWifiName() {
        String workSSID = NetUtils.getCurentWifiSSID(this);
        if (!TextUtils.isEmpty(workSSID)) {
            enter_wifiname_et.setText(workSSID);
        } else {
            showToast("获取wifi信息失败，请检查wifi");
        }
    }

    public void startLinking(String name, String pwd) {
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
            spf.edit().putString("workSSID", name).commit();
            spf.edit().putString("workSSIDPsw", pwd).commit();

            Intent intent = new Intent(this, GateWayLinkReadyAct.class);
            startActivity(intent);
            finish();
        }
        //ssid为wifi名，password为wifi密码，AC.DEVICE_ACTIVATOR_DEFAULT_TIMEOUT为AC默认的配网超时时间60s，具体可根据实际情况测试进行修改。
        /*deviceActivator.startAbleLink(wifiName, wifiPwd,  AC.DEVICE_ACTIVATOR_DEFAULT_TIMEOUT, new PayloadCallback<List<ACDeviceBind>>() {
            @Override
            public void success(List<ACDeviceBind> deviceBinds) {
                //成功后得到已激活设备的列表，从列表中得到物理id后可调用AC.bindMgr()的bindDevice接口进行绑定
            }

            @Override
            public void error(ACException e) {
                //根据e.getErrorCode()做不同的提示或处理，此处一般为1993配置超时错误
            }
        });*/
    }

    //    @OnClick(R.id.start_link_tv)
    //    public void next(){
    //        showToast("开始连接");
    //        //跳转到 正在连接界面
    //        Intent intent = new Intent(EnterWifiPwdActivity.this, IsLinkingActivity.class);
    //        startActivity(intent);
    //    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.start_link_tv:
                name = enter_wifiname_et.getText().toString();
                pwd = enter_wifipwd_et.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    showToast("请输入wifi名称");
                    return;
                }
//                if (!TextUtils.isEmpty(pwd)) {
                    if (video.equals("video")) {
                        //进入配置摄像头的界面
                        Intent smartLink = new Intent(EnterWifiPwdActivity.this, SmartLinkActivity1.class);
                        smartLink.putExtra("wifiname", name);
                        smartLink.putExtra("wifipwd", pwd);
                        startActivity(smartLink);
                    } else {
                        startLinking(name, pwd);
                    }
//                } else {
//                    showToast("请输入wifi密码");
//                }
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
